package com.cdd.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.commonutils.R;
import com.cdd.eduservice.entity.EduCourse;
import com.cdd.eduservice.entity.vo.CourseInfoVo;
import com.cdd.eduservice.entity.vo.CoursePublishVo;
import com.cdd.eduservice.entity.vo.CourseQuery;
import com.cdd.eduservice.entity.vo.TeacherQuery;
import com.cdd.eduservice.mapper.EduCourseMapper;
import com.cdd.eduservice.service.IEduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private IEduCourseService eduCourseService;


    // 添加课程
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        // 返回添加成功的课程ID
        String courseId = eduCourseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId",courseId);
    }

    // 根据课程ID查找
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId) {

        // 需要联表查询course和description
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);

        return R.ok().data("data",courseInfoVo);
    }

    // 修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    // 根据课程ID查询确认信息
    @GetMapping("getPublishInfo/{courseId}")
    public R getPublishInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = eduCourseService.getPublishInfo(courseId);
        return R.ok().data("data",coursePublishVo);
    }

    // 确认课程最终发布
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    // 查询所有课程
    @GetMapping("getAllCourse")
    public R getAllCourse() {
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }

    // 分页查询课程
    @GetMapping("getCourseLimit/{current}/{limit}")
    public R getCourseLimit(@PathVariable long current, @PathVariable long limit) {
        Page<EduCourse> page = new Page<>(current, limit);
        eduCourseService.page(page,null);
        long total = page.getTotal();
        List<EduCourse> records = page.getRecords();
        return R.ok().data("total",total).data("list",records);
    }

    // 按条件分页查询
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current,
                                 @PathVariable long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> page = new Page<>(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if(!StringUtils.isEmpty(title)) {
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)) {
            wrapper.eq("status",status);
        }
        wrapper.orderByDesc("gmt_create");
        eduCourseService.page(page, wrapper);
        long total = page.getTotal();
        List<EduCourse> records = page.getRecords();
        return R.ok().data("total",total).data("list",records);
    }

    // 删除课程
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }
}

